home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************/
- /* */
- /* Module: tohex.c - Translate any file to hex values */
- /* */
- /* Programmer: George R. Woodside */
- /* */
- /* Date: December 7, 1986 (No bombs, no Japanese attack!) */
- /* */
- /****************************************************************************/
-
- #include <stdio.h>
-
- /****************************************************************************/
- /* */
- /* Output mode control flag, set by entering an option on the */
- /* command line (-b, -w, or -l): */
- /* */
- /* 0 = bytes */
- /* 1 = words */
- /* 2 = longs */
- /* */
- /****************************************************************************/
-
- int mode = 1; /* default to words */
-
- main(argc,argv)
- int argc;
- char *argv[];
-
- {
-
- int i;
- FILE *fp;
-
- if (argc > 1) /* if multiple arguments, */
- {
- for (i = 1; i < argc; i++) /* then do each one */
- {
- if(strcmp(argv[i],"-b") == 0) /* if byte flag, */
- mode = 0; /* set byte dump mode */
- else if(strcmp(argv[i],"-w") == 0) /* if word flag, */
- mode = 1; /* set word dump mode */
- else if(strcmp(argv[i],"-l") == 0) /* if long flag, */
- mode = 2; /* set long dump mode */
- else if( (fp = fopenb(argv[i],"r") ) == NULL) /* if open error, */
- {
- printf("%s: Unable to open %s \n",argv[0],argv[i]);
- printf("\n\n");
- continue;
- } /* end file open error */
- else /* if file opened OK, */
- {
- tohex(fp); /* print it */
- } /* end file opened OK */
- } /* end for one argument */
- } /* end for all arguments */
- else /* if no arguments, */
- tohex(stdin); /* dump standard in */
- } /* end main */
-
- /****************************************************************************/
- /* */
- /* See which format to dump in */
- /* */
- /****************************************************************************/
-
- tohex(fp)
- FILE *fp; /* file to dump */
- {
- if(mode == 0)
- bdump(fp); /* byte dump */
-
- if(mode == 1)
- wdump(fp); /* word dump */
-
- if(mode == 2)
- ldump(fp); /* long dump */
- } /* end tohex */
-
- /****************************************************************************/
- /* */
- /* Byte dump */
- /* */
- /****************************************************************************/
-
- bdump(fp)
- FILE *fp; /* file to dump */
-
- {
- register int i=0; /* set a counter */
- register int ch; /* byte or word dump */
-
- printf(" "); /* set the lead-in */
- while(( ch=fgetc(fp)) != EOF) /* for each character, */
- {
- printf("0x%02x, ",(int) (ch & 0xff) ); /* print the byte */
- if(i++ == 12)
- {
- i = 0; /* reset the line count */
- printf("\n "); /* move down a line */
- }
- }
- if (i != 0) /* if any left over bytes, */
- printf("\n"); /* a final newline */
-
- fclose(fp); /* close input file */
- }
-
- /****************************************************************************/
- /* */
- /* Word dump */
- /* */
- /****************************************************************************/
-
- wdump(fp)
- FILE *fp; /* file to dump */
-
- {
- register int i = 0; /* set a counter */
- register int ch; /* byte or word dump */
- register int wch = 0; /* word dump */
-
- printf(" "); /* set the lead-in */
- while(( ch=fgetc(fp)) != EOF) /* for each character, */
- {
- wch = (wch << 8) + ch; /* add in the new byte */
- if((++i & 1) == 0) /* if we have a word, */
- {
- printf("0x%04x, ",wch ); /* print the word */
- wch = 0; /* and clear the value */
- }
- if(i == 16) /* if at end of a line, */
- {
- i = 0; /* reset the line count */
- printf("\n "); /* move down a line */
- }
- } /* end of file */
- if(i & 1) /* if an odd byte left over, */
- printf("0x%04x ",wch << 8 ); /* print the byte */
-
- if (i != 0) /* if any left over bytes, */
- printf("\n"); /* a final newline */
-
- fclose(fp); /* close the file */
- }
-
- /****************************************************************************/
- /* */
- /* Long dump */
- /* */
- /****************************************************************************/
-
- ldump(fp)
- FILE *fp; /* file to dump */
-
- {
- register int i = 0; /* set a counter */
- register int ch; /* byte or word dump */
- register long lch = 0L; /* long dump */
-
- printf(" "); /* set the lead-in */
- while(( ch=fgetc(fp)) != EOF) /* for each character, */
- {
- lch = (lch << 8) + ch; /* add in the new byte */
- if((++i & 3) == 0) /* if we have a long word, */
- {
- printf("0x%08X, ",lch ); /* print the long */
- lch = 0L; /* and clear the value */
- }
- if(i == 16) /* if at end of a line, */
- {
- i = 0; /* reset the line count */
- printf("\n "); /* move down a line */
- }
- } /* end of file */
- if((i & 3) == 1) /* if one odd byte left over, */
- printf("0x%08X ",lch << 24 ); /* print the byte */
-
- if((i & 3) == 2) /* if two odd bytes left over, */
- printf("0x%08X ",lch << 16 ); /* print them */
-
- if((i & 3) == 3) /* if three odd bytes left over, */
- printf("0x%08X ",lch << 8 ); /* print them */
-
- if (i != 0) /* if any left over bytes, */
- printf("\n"); /* a final newline */
-
- fclose(fp); /* close the file */
- }
-